package com.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alipay.api.AlipayApiException;
import com.config.RequestAliPay;
import com.constans.PayConstants;
import com.entity.Pay;
import com.service.IAliPayService;
import com.service.IPayService;
import com.utils.ZhifubaoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/***
 * @Author Ms.发发
 * @ClassName ALiPayServiceImpl
 * @Date 2024/12/19 9:05
 * @Description
 **/

@Service
@Slf4j
public class AliPayServiceImpl implements IAliPayService {

    @Autowired
    private IPayService payService;
    @Override
    public Map aliPay(RequestAliPay requestAliPay) throws AlipayApiException {
        Boolean flag = true;

        Map payResultMap = ZhifubaoUtil.pay(requestAliPay);

        //添加数据库操作 TODO:   支付数据库  插入一条预支付记录
        log.info("==[AliPayServiceImpl] payResultMap@data={}",payResultMap);

        //TODO:   构建插入Pay对象
        Pay pay = Pay.builder()
                .orderNo(requestAliPay.getOutTradeNo())
                .payStatus(PayConstants.PAY_WAIT_QR_STATUS)
                .payAmount(requestAliPay.getTotalAmount())
                .tradeType("支付宝")
                .channelId("CIDX030723ZFB")
                .channelName("MyChannelName")
                .channelStatus(2)
                .merchantId("11473623")
                .codeUrl(payResultMap.toString())
                .build();

        log.info("== 构建插入Pay对象： data={}",pay.toString());

        int insertPayCount = this.payService.insertPay(pay);
        log.info("== 改变行数： insertPayCount={}",insertPayCount);
        if(ObjectUtil.isNotNull(payResultMap)&& insertPayCount==1){
            return payResultMap;
        }
        return null;
    }
}
